package com.etool.action.remotessh;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;

import com.etool.commons.rmtshell.RmtShellExecutor;
import com.etool.commons.utils.AjaxUtils;
import com.etool.commons.utils.DateUtils;

import com.etool.modules.auth.model.AuthWorker;
import com.etool.modules.auth.model.NoRequireAuth;
import com.etool.modules.auth.service.CurrentWorkerAware;
import com.etool.modules.curd.service.CurdService;
import com.opensymphony.xwork2.ActionSupport;
@NoRequireAuth
public class RemoteSSHAction extends ActionSupport implements CurrentWorkerAware{
	
	private HttpServletResponse response=ServletActionContext.getResponse();
	
	private AuthWorker authWorker;
	
	private String ip="10.10.11.132";
	private int port=16333;
	private String username;
	private String password;
	private String cmd="ps -aux";
	private String name;
	
	private CurdService curdService;
	
	
	List<Map<String, Object>> ips;
	
	@Action("remotessh")
	public String index()
	{
		ips=curdService.read("remote_ssh_ip", null, 0, 10000,null);
		return SUCCESS;
	}
	

	@Action("remotessh-menu")
	public String menu()
	{
		Map<String, Object> map1=new HashMap<String, Object>();
		map1.put("id", 1);
		map1.put("parentId", 0);
		map1.put("name", "远程shell");
		map1.put("url", "/remotessh/remotessh.action");
		
		Map<String, Object> map2=new HashMap<String, Object>();
		map2.put("id", 1);
		map2.put("parentId", 0);
		map2.put("name", "ip配置");
		map2.put("url", "/curd/index.action?target=remote_ssh_ip");
		
		
		List<Map<String, Object>> list=new ArrayList<Map<String,Object>>();
		list.add(map1);
		list.add(map2);
		
		
		Map<String, Object> res=new HashMap<String, Object>();
		
		res.put("menu", list);
		
		AjaxUtils.writeJson(response, res);
		return NONE;
	}
	
	@Action("execute-cmd")
	public String executeCmd()
	{
		Date begin=new Date();
		RmtShellExecutor rmt=new RmtShellExecutor(ip, port, username, password);
		Map<String, Object> res= rmt.exec(cmd.replaceAll("\r\n", "\n").trim());
		Date end=new Date();
		res.put("ip", ip);
		res.put("port", port);
		res.put("username", username);
		res.put("cmd",cmd);
		res.put("worker", authWorker.getPinyin());
		res.put("startTime", DateUtils.date2str(begin));
		res.put("endTime", DateUtils.date2str(end));
		
		curdService.create("remote_ssh_log", res);
		
		AjaxUtils.writeJsonEscapeHtml(response, res);
		return NONE;
	}
	
	public String shortcut()
	{
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("ip", ip);
		map.put("port", port);
		map.put("username", username);
		map.put("password", password);
		map.put("cmd",cmd);
		map.put("worker","");
		curdService.create("remote_shortcut", map);
		AjaxUtils.writeJson(response,Collections.singletonMap("success", (Object)true));
		return NONE;
	}
	
	public String getIp() {
		return ip;
	}
	public void setIp(String ip) {
		this.ip = ip;
	}

	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getCmd() {
		return cmd;
	}
	public void setCmd(String cmd) {
		this.cmd = cmd;
	}

	public CurdService getCurdService() {
		return curdService;
	}

	public void setCurdService(CurdService curdService) {
		this.curdService = curdService;
	}

	public List<Map<String, Object>> getIps() {
		return ips;
	}

	public void setIps(List<Map<String, Object>> ips) {
		this.ips = ips;
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setAuthWorker(AuthWorker authWorker) {
		this.authWorker=authWorker;
	}
}
